

package com.hexinfo.dmpro.dev.data.transfer.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hexinfo.dmpro.common.annotation.AnonymousAccess;
import com.hexinfo.dmpro.dev.data.transfer.model.DevDataExportList;
import com.hexinfo.dmpro.dev.data.transfer.model.DevDataImportList;
import com.hexinfo.dmpro.dev.data.transfer.service.IDevDataImportListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.io.File;
import java.nio.file.Files;
import java.util.List;


/**
 * 表导入任务表
 *
 * @author yyh.huang
 * @date 2023-10-25 19:15:04
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/api/dev/data/import/list" )
@Api(value = "devdataimportlist", tags = "表导入任务表管理")
public class DevDataImportListController {

    private final IDevDataImportListService devDataImportListService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param devDataImportList 表导入任务表
     * WrapperEnhancer.like(devDataImportList); //自动拼接存在值sql的方法
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public ResponseEntity<Object> getDevDataImportListPage(Page page, DevDataImportList devDataImportList) {
        return ResponseEntity.ok(devDataImportListService.queryList(page,devDataImportList));
    }

    @ApiOperation(value = "开发数据导入执行完成更新清单信息", notes = "开发数据导入执行完成更新清单信息")
    @PostMapping
    @AnonymousAccess
    public ResponseEntity<Object> updateStatus(@RequestBody DevDataImportList devDataImportList) {
        log.info("更新开发数据导入清单信息：{}", devDataImportList.toString());
        devDataImportListService.updateStatus(devDataImportList);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation(value = "开发数据导入清单重跑", notes = "开发数据导入清单重跑")
    @PutMapping("/restart/{ids}")
    @AnonymousAccess
    public ResponseEntity<Object> restart(@PathVariable("ids") @NotNull List<String> ids) {
        log.info("开发数据导入清单重跑：{}", StrUtil.join(",", ids));
        devDataImportListService.restart(ids);
        return ResponseEntity.ok("ok");
    }
    @ApiOperation("开发数据导入表清单")
    @GetMapping(value = "/download")
    public ResponseEntity<Resource> download(DevDataImportList devDataImportList) throws Exception {
        String downloadFile = devDataImportListService.download(devDataImportList);
        byte[] bytes = Files.readAllBytes(new File(downloadFile).toPath());
        ByteArrayResource bar = new ByteArrayResource(bytes);
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .header("Content-disposition", "attachment; filename=文件.xlsx")
                .body(bar);
    }
}
